package com.jdd.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.dao.entity.TDeviceCmdResponse;
import com.jdd.dao.entity.TDeviceCmdStatus;
import com.jdd.dao.mapper.TDeviceCmdResponseMapper;
import com.jdd.dao.mapper.TDeviceCmdStatusMapper;
import com.jdd.dao.service.TDeviceCmdResponseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 设备指令回复表 服务实现类
 * </p>
 *
 * @author zly
 * @since 2022-03-24
 */
@Service
public class TDeviceCmdResponseServiceImpl extends ServiceImpl<TDeviceCmdResponseMapper, TDeviceCmdResponse> implements TDeviceCmdResponseService {
    @Resource
    private TDeviceCmdStatusMapper cmdStatusMapper;

    @Resource
    private TDeviceCmdResponseMapper cmdResponseMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveCmdResponseAndDelCmdStatus(TDeviceCmdResponse cmdResponse) {
        QueryWrapper<TDeviceCmdStatus> cmdStatusQueryWrapper = new QueryWrapper<>();
        cmdStatusQueryWrapper.eq("cmd_batch_id", cmdResponse.getCmdBatchId());
        cmdStatusMapper.delete(cmdStatusQueryWrapper);
        System.out.println("删除完成");
        this.baseMapper.insert(cmdResponse);
        System.out.println("插入完成"+cmdResponse);
    }

    @Override
    public IPage<TDeviceCmdResponse> findList(Page<TDeviceCmdResponse> page, TDeviceCmdResponse tDeviceCmdResponse) {
        List<String> projectList = LoginUserUtils.getProjectCodeList();
        IPage<TDeviceCmdResponse> iPage = cmdResponseMapper.findList(page,tDeviceCmdResponse,projectList);
        return iPage;
    }
}
